﻿
/*
 * Add a ZERO document for UDS_Flow_Files to maintain the FK constraint
 */
IF NOT EXISTS ( SELECT 1 FROM dbo.UDS_Flow_Style_Data WHERE Doc_ID = 0 )
BEGIN
    SET IDENTITY_INSERT dbo.UDS_Flow_Style_Data ON;
    INSERT INTO dbo.UDS_Flow_Style_Data (Doc_ID, a) VALUES(0, '{00000000-0000-1234-0000-000000000000}');
    SET IDENTITY_INSERT dbo.UDS_Flow_Style_Data OFF;
END
GO


/*
 * Rebuild FK_UDS_Flow_Files_DocID constraint to reference UDS_Flow_Style_Data table.
 */
IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE [object_id] = OBJECT_ID(N'[dbo].[FK_UDS_Flow_Files_DocID]') AND [parent_object_id] = OBJECT_ID(N'[dbo].[UDS_Flow_Files]'))
    ALTER TABLE [dbo].[UDS_Flow_Files] DROP CONSTRAINT [FK_UDS_Flow_Files_DocID];
ALTER TABLE [dbo].[UDS_Flow_Files] ADD CONSTRAINT [FK_UDS_Flow_Files_DocID] FOREIGN KEY(DocID) REFERENCES [dbo].[UDS_Flow_Style_Data] (Doc_ID);
GO
